package com.leyou.system.controller;

import com.leyou.common.auth.domain.Payload;
import com.leyou.common.auth.utils.JwtUtils;
import com.leyou.common.domain.PageResult;
import com.leyou.common.utils.CookieUtils;
import com.leyou.system.config.JwtProperties;
import com.leyou.system.domain.UserVo;
import com.leyou.system.dto.PermissionDTO;
import com.leyou.system.service.PermissionService;
import com.leyou.system.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Date: 2019/11/28
 * Author: L
 * Description: 权限控制器
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    // 注入权限service
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private JwtProperties prop;

    /**
     * 查询权限列表
     */
    //@Secured("ROLE_USER")
    @GetMapping("/page")
    public ResponseEntity<PageResult<PermissionDTO>> PermissionPageQuery(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                         @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                                         @RequestParam(value = "key", required = false) String key,
                                                                         @RequestParam("sortBy") String sortBy,
                                                                         @RequestParam("parentId") Integer parentId,
                                                                         @RequestParam(value = "desc", defaultValue = "false") Boolean desc) {

        PageResult<PermissionDTO> pageResult = permissionService.PermissionPageQuery(page, rows, key, sortBy, desc,parentId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据父id查询权限
     * @param pid
     * @return
     */
    @GetMapping("/of/parent")
    //@Secured("ROLE_USER")
    public ResponseEntity<List<PermissionDTO>> queryPermissionByPid(@RequestParam("pid") Integer pid) {
        List<PermissionDTO> list = permissionService.queryPermissionByPid(pid);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据角色id查询权限
     * @return
     */
    @GetMapping("/of/role")
    //@Secured("ROLE_USER")
    public ResponseEntity<List<PermissionDTO>> queryByRoleId(@RequestParam("rid") Integer rid) {
        List<PermissionDTO> permissionDTOS = permissionService.queryByRoleId(rid);
        return ResponseEntity.ok(permissionDTOS);
    }

    /**
     * 获取所有权限
     * @return
     */
    @GetMapping
    //@Secured("ROLE_USER")
    public ResponseEntity<List<PermissionDTO>> queryAll() {
        List<PermissionDTO> permissionDTOList = permissionService.queryAll();
        return ResponseEntity.status(HttpStatus.OK).body(permissionDTOList);
    }


    /*
    查询所有的一级菜单
     */

    @GetMapping("/of/pid")
    //@Secured("ROLE_USER")
    public ResponseEntity<List<PermissionDTO>> findAllPermissionByParentId(@RequestParam("parentId") Integer parentId){
        List<PermissionDTO> list = permissionService.findAllPermissionByParentId(parentId);
        return ResponseEntity.ok(list);
    }


    /*
    增加菜单
     */
    @PostMapping
    //@Secured("ROLE_USER")
    public ResponseEntity<Void> addPermission(@RequestBody PermissionDTO permissionDTO){
        permissionService.addPermission(permissionDTO);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    /*
    根据id查询菜单
     */
    @GetMapping("/{id}")
    //@Secured("ROLE_USER")
    public ResponseEntity<PermissionDTO> findPermissionById(@PathVariable("id") Integer id ){
        PermissionDTO permissionDTO = permissionService.findPermissionById(id);
        return ResponseEntity.ok(permissionDTO);
    }


    @GetMapping("/menus")
    public ResponseEntity<List<PermissionVo>> findMenus(HttpServletRequest request){
        //解析token获取用户id
        String token = CookieUtils.getCookieValue(request, "LY_TOKEN");
        Payload<UserVo> payload = JwtUtils.getInfoFromToken(token, prop.getPublicKey(), UserVo.class);
        //获取用户信息
        UserVo userInfo = payload.getUserInfo();
        //得到用户的id
        Integer id = userInfo.getId();
        //查询用户id的权限或菜单
        List<PermissionVo> list = permissionService.findPermissionByUserId(id);
        return ResponseEntity.ok(list);
    }


    /**
     * 根据用户UserId，查询权限
     *//*
    @GetMapping("/of/userId")
    public ResponseEntity<List<PermissionDTO>> findPermissionByUserId(@RequestParam("uid") Integer uid){
        List<PermissionDTO> list = permissionService.findPermissionByUserId(uid);
        return ResponseEntity.ok(list);
    }*/

}
